const base64 = require('../../utils/uploadImg/base64.js');
const app = getApp()
let API_BASE_URL = app.globalData.baseURL.baseURL

const {
  get,
  post
} = require('../../request/index')

Page({

  data: {
    code: "",
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    loginShow: false,
    showOneButtonDialog: false,
    oneButton: [{
      text: '确定'
    }],
    loginuser: "一键登录",
    error: '',
    formData: {

    },
    rules: [{
      name: 'account',
      rules: {
        required: true,
        message: '账号是必填项'
      },
    },
    {
      name: 'password',
      rules: {
        required: true,
        message: '密码是必填项'
      },
    }
    ]
  },
  onUnload() {
    this.setData({
      loginShow: false

    })
  },
  tapOneDialogButton(e) {
    this.setData({
      showOneButtonDialog: false
    })
  },



  formInputChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },
  onLoad() {
    this.gobayto()
  },

  gobayto() {
    let that = this
    wx.login({
      success(res) {
        if (res.code) {
          that.setData({
            code: res.code,
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },


  getPhoneNumber(e) {

    this.gobayto()

    let that = this;
    const {
      errMsg,
      iv,
      encryptedData
    } = e.detail
    if (errMsg == "getPhoneNumber:fail user deny") {
      return
    }
    that.setData({
      loginShow: true
    })

    wx.checkSession({
      success(res) {
        if (that.data.code) {
          that.setData({
            loginShow: false
          })
          // 登录接口
          post('/auth/wxLogin', {
            code: that.data.code,
            userId: that.data.prolist.userId || '',
          }).then(res => {
            console.log(res);
            if (res.code === 500) {
              wx.showToast({
                title: '首次登录请通过账号密码登录后授权，才能一键登录',
                duration: 5000,
                icon: 'none',
                mask: true
              })
              setTimeout(() => {
                that.submitForm()
              }, 2000)
            }else if (res.code === 200) {
              console.log(111);
              wx.setStorageSync('token', JSON.stringify(res.data.access_token));
              wx.showToast({
                title: '登录成功',
                duration: 3000,
                icon: 'success',
                mask: true
              })
              wx.switchTab({
                url: '/pages/grid/grid'
              })
            }
          })
        } else {
          
          console.log('登录失败！' + res.errMsg)
        }


      },
      fail() {
        that.gobayto()
      }

    })


  },


  getinfo(openid) {
    let that = this
    wx.request({
      url: `${API_BASE_URL}/uaa/oauth/token?username=${openid}&password=8&grant_type=password&scope=app`, //仅为示例，并非真实的接口地址
      method: "POST",
      header: {
        'content-type': 'application/json',
        'Authorization': "Basic " + (base64.encode("app:123456")) // 默认值
      },
      success(res) {
        let {
          data
        } = res

        wx.setStorageSync('token', JSON.stringify(data.access_token));

        wx.showToast({
          title: '登录成功',
          duration: 5000,
          icon: 'success',
          mask: true
        })
        that.setData({
          loginShow: false

        })
        // return

        wx.switchTab({
          url: '/pages/grid/grid'
        })
      }
    })

  },
  submitForm() {
    wx.navigateTo({
      url: '/pages/account/account',
    })
  }

})